<template>
  <div class="my-two">
    <div style="background-color: #FEF4F4;padding-bottom: 20px">
      <el-carousel style="padding-left: 50px;padding-right: 50px;" height="600px">
        <el-carousel-item v-for="item in bookRecList" :key="item">
          <router-link :to="{path:'/bookDetail',query:{id:item.bookId}}">
            <Advertisement :my-data="item" style="margin: auto"></Advertisement>
          </router-link>
        </el-carousel-item>
      </el-carousel>

      <div style="text-align: center;margin-top: 50px;">
        <h2>有史以来最受欢迎的书</h2>
      </div>

      <div style="margin-top: 50px;padding-left: 5%;padding-right: 5%">
        <swiper
            :modules="modules"
            :slides-per-view="5"
            :space-between="10"
            :autoplay="{ delay: 1500, disableOnInteraction: false}"
            :loop="true"
            :centeredSlides="true"
            @swiper="onSwiper"
            @slideChange="onSlideChange"
        >
          <swiper-slide v-for="item in bookSellWellList">
            <router-link :to="{path:'/bookDetail',query:{id:item.bookId}}">
              <BookSingle style="margin-left: 30px" :book-data="item"></BookSingle>
            </router-link>
          </swiper-slide>
        </swiper>
      </div>
    </div>
    <LatestPublished class="my-two"></LatestPublished>
  </div>
</template>

<script setup>
import BookSingle from "../Slither/BookSingle.vue";
import { Swiper, SwiperSlide } from 'swiper/vue';
import 'swiper/css';
import { Autoplay, Navigation, A11y } from 'swiper';
import 'swiper/css'
import 'swiper/css/navigation'
import 'swiper/css/pagination'
import LatestPublished from "../Composite/LatestPublished.vue";
import Advertisement from "../Slither/Advertisement.vue";
import store from "../../store/index.js";
import axios from "../../axios/index.js";
import {ElMessage} from "element-plus";
import {ref} from "vue";

const  modules = [Autoplay, Navigation, A11y]

const onSwiper = (swiper) => {
  //console.log(swiper);
};
const onSlideChange = () => {
  //console.log('slide change');
};

let bookRecList = ref([])

const getHomeRecommend = ()=>{
  let type = ''
  if (store.getters.getToken === null || store.getters.getToken === '') {
    //console.log("token 空")
    type = "small";
  }else{
    //console.log("token " + store.getters.getToken)
    type = "big";
  }
  type = "small";
  axios.get('/book/recommend/'+ type).then(res => {
    //console.log(res.data.data);
    bookRecList.value = res.data.data;
  }).catch(error => {
    ElMessage.error(error)
  })
}
getHomeRecommend();

let bookSellWellList = ref([])
const getsSellWellBook = ()=>{
  axios.get('/book/well').then(res => {
    //console.log(res.data.data);
    bookSellWellList.value = res.data.data;
  }).catch(error => {
    ElMessage.error(error)
  })
}
getsSellWellBook();
</script>

<style scoped>
.my-two{
  /*background-color: #fafafa;*/
  padding-bottom: 30px;
}
a{
  text-decoration: none;
  color: black;
}
</style>
